package com.tnovoselec.server.news.beans;

import java.util.ArrayList;
import java.util.List;

public class BugItem extends IItem {

	private String title;
	private String link;
	private String content;
	private String description;
	private List<String> images;
	private String pubDate;

	public BugItem() {
		images = new ArrayList<String>();
	}

	@Override
	public String getTitle() {
		return title;
	}

	@Override
	public String getDescription() {
		return description;
	}

	@Override
	public List<String> getImages() {
		return images;
	}

	@Override
	public String getPubDate() {
		return pubDate;
	}

	@Override
	public String getLink() {
		return link;
	}

	@Override
	public String getContent() {
		return content;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setLink(String link) {
		this.link = link;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setDescription(String description) {
		// this.description = description;
		handleDescription(description);
	}

	public void setImages(List<String> images) {
		this.images = images;
	}

	public void setPubDate(String pubDate) {
		this.pubDate = pubDate;
	}

	@Override
	public String getTitleTagName() {

		return "title";
	}

	@Override
	public String getDescriptionTagName() {
		return "description";
	}

	@Override
	public String getImagesTagName() {
		return "enclosure";
	}

	@Override
	public String getPubDateTagName() {
		return "pubDate";
	}

	@Override
	public String getLinkTagName() {
		return "link";
	}

	@Override
	public String getContentTagName() {
		return "";
	}

	@Override
	public String getItemTagName() {
		return "item";
	}

	private void handleDescription(String desc) {
		while (desc.contains("<")) {
			if (desc.contains("img src='")) {
				String imgUrl = desc.substring(desc.indexOf("img src='") + "img src='".length(),
						desc.indexOf("'", desc.indexOf("img src='") + "img src='".length()));
				images.add(imgUrl);
				String garbage = desc.substring(desc.indexOf("<"), desc.indexOf(">") + ">".length());
				desc = desc.replace(garbage, "");
			} else if (desc.contains("img src=\"")) {
				String imgUrl = desc.substring(desc.indexOf("img src=\"") + "img src=\"".length(),
						desc.indexOf("\"", desc.indexOf("img src=\"") + "img src=\"".length()));
				images.add(imgUrl);
				String garbage = desc.substring(desc.indexOf("<"), desc.indexOf(">") + ">".length());
				desc = desc.replace(garbage, "");
			}
		}
		description = desc;

	}

	@Override
	public void resetImages() {
		images = new ArrayList<String>();

	}

}
